// AlarmTrayIcon.cpp
#include "AlarmTrayIcon.h"
#include <QApplication>

AlarmTrayIcon::AlarmTrayIcon(QObject *parent)
    : QSystemTrayIcon(parent) {

    // 加载图标资源
    normalIcon = QIcon(":/icons/alarm_off.png");
    alertIcon = QIcon(":/icons/alarm_on.png");

    // 初始化托盘图标
    setIcon(normalIcon);
    setVisible(true);

    // 配置定时器
    flashTimer = new QTimer(this);
    connect(flashTimer, &QTimer::timeout, this, &AlarmTrayIcon::onTimeout);
}

void AlarmTrayIcon::startFlashing() {
    if(!isFlashing) {
        isFlashing = true;
        flashTimer->start(500);  // 500ms切换间隔
        setIcon(alertIcon);      // 初始显示警报图标
    }
}

void AlarmTrayIcon::stopFlashing() {
    if(isFlashing) {
        isFlashing = false;
        flashTimer->stop();
        setIcon(normalIcon);     // 恢复默认图标
    }
}

void AlarmTrayIcon::onTimeout() {
    static bool state = false;
    state = !state;

    if(state) {
        setIcon(alertIcon);
        // 触发系统通知
        showMessage("闹钟提醒", "当前闹钟时间到！", QSystemTrayIcon::Critical, 2000);
    } else {
        setIcon(normalIcon);
    }
}
